package com.itheima.googleplay9.ui.fragment;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.itheima.googleplay9.R;
import com.itheima.googleplay9.api.DataLoader;
import com.itheima.googleplay9.api.Url;
import com.itheima.googleplay9.ui.view.FlowLayout;
import com.itheima.googleplay9.util.ColorUtil;
import com.itheima.googleplay9.util.DrawableUtil;
import com.itheima.googleplay9.util.ToastUtil;
import com.itheima.googleplay9.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lxj on 2016/5/23.
 */
public class HotFragment extends BaseFragment {
    FlowLayout flowLayout;
    private ArrayList<String> list;
    private ScrollView scrollView;
    private int hPadding,vPadding;
    @Override
    protected View getSuccessView() {
        scrollView = new ScrollView(getActivity());
        flowLayout = new FlowLayout(getActivity());

        hPadding = Utils.getDimens(R.dimen.dp12);
        vPadding = Utils.getDimens(R.dimen.dp9);

        //1.设置padding
        int padding = Utils.getDimens(R.dimen.dp15);
        flowLayout.setPadding(padding,padding,padding,padding);


        scrollView.addView(flowLayout);
        return scrollView;
    }

    @Override
    protected Object onLoad() {
        list = (ArrayList<String>) DataLoader.getInstance().loadListData(Url.Hot,new TypeToken<List<String>>(){}.getType());

        if(list !=null){

            Utils.runOnUIThread(new Runnable() {
                @Override
                public void run() {

                    //给flowLayout添加子View
                    for (int i = 0; i< list.size(); i++){
                        final TextView textView = new TextView(getActivity());
                        textView.setText(list.get(i));
                        textView.setGravity(Gravity.CENTER);
                        textView.setTextColor(Color.WHITE);
                        textView.setPadding(hPadding,vPadding,hPadding,vPadding);
//                        textView.setBackgroundColor(ColorUtil.randomBeautifulColor());
                        //动态创建默认和按下状态对应的图片
                        GradientDrawable normal = DrawableUtil.generateDrawable(vPadding);
                        GradientDrawable pressed = DrawableUtil.generateDrawable(vPadding);
                        textView.setBackgroundDrawable(DrawableUtil.generateSelector(normal,pressed));

                        //将textView加入到FlowLayout中
                        flowLayout.addView(textView);

                        //添加点击事件
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ToastUtil.shwoToast(textView.getText().toString());
                            }
                        });
                    }
                }
            });

        }
        return list;
    }
}
